package com.example.goodsmanagementcabinet.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.goodsmanagementcabinet.R;
import com.example.goodsmanagementcabinet.bean.UserBean;
import com.example.goodsmanagementcabinet.otto.OttoManager;
import com.example.goodsmanagementcabinet.otto.OttoMsg;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;


public class UserListAdapter extends BaseAdapter {
    private Context mContext;
    private List mArrayList = new ArrayList();

    private boolean isIntent = false;

    public boolean isIntent() {
        return isIntent;
    }

    public UserListAdapter(Context context) {
        this.mContext = context;
    }

    public void notifyRefresh(List<UserBean> list) {
        mArrayList.clear();
        mArrayList.addAll(list);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return (mArrayList != null && mArrayList.size() == 0) ? 0 : mArrayList.size();
    }

    @Override
    public Object getItem(int arg0) {
        return mArrayList.get(arg0);
    }

    @Override
    public long getItemId(int arg0) {
        return arg0;
    }

    @Override
    public View getView(final int position, View currentView, ViewGroup arg2) {
        HolderView holderView = null;
        if (currentView == null) {
            holderView = new HolderView();
            currentView = LayoutInflater.from(mContext).inflate(R.layout.item_userlists, arg2, false);
            holderView.mNameTv = (TextView) currentView.findViewById(R.id.mNameTv);
            holderView.mNoTv = (TextView) currentView.findViewById(R.id.mNoTv);
            holderView.face_text = (TextView) currentView.findViewById(R.id.face_collent_text);
            holderView.finger_text = (TextView) currentView.findViewById(R.id.finger_collent_text);
            holderView.mCollectFingerBut = currentView.findViewById(R.id.mCollectFingerBut);
            holderView.mCollectFaceBut = currentView.findViewById(R.id.mCollectFaceBut);
            holderView.imageView = currentView.findViewById(R.id.mPhotoIv);
            holderView.imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            currentView.setTag(holderView);
        } else {
            holderView = (HolderView) currentView.getTag();
        }
        final UserBean myuser = (UserBean) mArrayList.get(position);
        if (myuser != null) {
            holderView.mNameTv.setText(myuser.getNickname());
            holderView.mNoTv.setText(myuser.getUserId());
            if (TextUtils.equals(myuser.getStoreAt() ,"1")) {
                holderView.mCollectFingerBut.setBackgroundResource(R.drawable.ripple_imagebutton_finish);
                holderView.finger_text.setText("已采集");
            } else {
                holderView.mCollectFingerBut.setBackgroundResource(R.drawable.ripple_imagebutton);
                holderView.finger_text.setText("指纹采集");
            }
            holderView.mCollectFingerBut.setEnabled(true);
            holderView.mCollectFingerBut.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    System.out.println("---->click");
                    if (TextUtils.equals(myuser.getStoreAt() ,"1")) {
                        OttoManager.getInstance().deliverMsg(OttoMsg.OTTO_REINSERT_FINGER,myuser);
                    } else {
                        OttoManager.getInstance().deliverMsg(OttoMsg.OTTO_INSERT_FINGER,myuser);
                    }
                }
            });
    }
        return currentView;

    }

    public static Bitmap getLoacalBitmap(String url) {
        try {
            FileInputStream fis = new FileInputStream(url);
            return BitmapFactory.decodeStream(fis);  ///把流转化为Bitmap图片

        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return null;
        }
    }

    public class HolderView {
        private TextView mNameTv, mNoTv;
        private TextView mDeptName;
        private TextView face_text;
        private TextView finger_text;
        private LinearLayout mCollectFingerBut;
        private LinearLayout mCollectFaceBut;
        private ImageView imageView;

    }


}
